<?php
// +----------------------------------------------------------------------
// | ranking [ 捐款平台公款系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://localhost.ranking.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 七秒の记忆 <Majw998@Gmail.com>
// +----------------------------------------------------------------------
namespace Home\Model;
use Think\Model;

class ConfigModel extends Model{
	protected $_validate = array(
		array('name', 'require', '配置值不能为空', self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
	);

	protected $_auto = array(
		array('create_time', NOW_TIME, self::MODEL_INSERT),
		array('update_time', NOW_TIME, self::MODEL_BOTH),
	);

	public function update($data)
	{
		$info       = array();
		$info['e']  = 1;

		if(!$this->create($data)) {
			$info['e']    = -1;
			$info['info'] = $this->getError();
			return $info;
		}

		if($data['id']) {
			$info['info'] = $this->save();
		} else {
			$info['info'] = $this->add();
		}
		return $info;
	}

	
	public function setVal($data)
	{	

		foreach ($data as $key => $value) {
			if(!empty($value)) {
				$map['key_name'] = $key;
				$info[$key] = $this->where($map)->save(array('key_value' => $value));
			}
		}

		if(in_array(false, $info, true)) {
			$info = array();
			$info['e'] = -1;
			return $info['info'] = '部分更新失败，请重试';
		}
		$info['e'] = 1;
		$info['info'] = '操作成功';
		return $info;
	}


	public function _lists(){
		$map['is_delete'] = 1;
		$data 			  = $this->where($map)->select();
		$config 		  = array();
		foreach ($data as $key => $value) {
			$config[$value['key_name']] = $value['key_value'];
		}
		return $config;
	}

	public function getVal($key){
		return $this->where(array('key_name' => $key, 'is_delete' => 1))->getField('key_value');
	}
}